<?php

error_reporting(E_ALL);
ini_set('display_errors', '1');
header('Content-type: text/html; charset=UTF8');
if(empty($_FILES["file"]["tmp_name"])){
	echo "<script>alert('请上传文件');location.href='index.php';</script>";
    exit();
}
include_once 'configuration.php';
include_once 'includes.php';
if (($_FILES["file"]["size"] < 50000000))
{
	if ($_FILES["file"]["error"] > 0)
	{
		echo "Error: " . $_FILES["file"]["error"] . "<br />";
		echo "<script>alert('文件上传失败');location.href='index.php';</script>";
	}
	else
	{
		$filename=$_FILES["file"]["name"];
		$filetype=$_FILES["file"]["type"];
		$filesize=$_FILES["file"]["size"] / 1024 ;
		$filepath=$_FILES["file"]["tmp_name"];
		$md5value = md5_file($filepath);
		$sha1value=sha1_file($filepath);
		$sig_file=$tablePreStr."file";
		dbtarget('r',$dbServs);
		$dbo=new dbex;
		$sql="select * from sig_file where mval='$md5value' or sha1val='$sha1value'";
		$file_info=$dbo->getRow($sql);
		if(!empty($file_info)){
			echo "<script>alert('这个文件上传过，是否查看以前的记录');</script>";
			$tpl->assign("file_info",$file_info);
			$tpl->display("oldscan.html");
		}else{
		
		
		$return=exec("core/fastavweb ".$filepath." 6 core/sig.txt core/fastdir 65536 131072",$out);
		//if($out)
		$matches=array();
		preg_match_all('|:(.*);|U', $return, $matches);
		$result=0;
		if($matches[1][0]!="novirus")
			$result=1;
			//     for($i=0;$i<5;$i++),clamtime , sigscantime , clamres ,sigscanres, clameffi,sigeffi
				//        echo "matches:".$matches[1][$i];$matches[1][3],$matches[1][4],$matches[1][0],$matches[1][1],$matches[1][5],$matches[1][6]
		dbtarget('w',$dbServs);
		$dbo=new dbex;
		$clamtime=$matches[1][3];
		$sigscantime=$matches[1][4];
		$clamres=$matches[1][0];
		$sigscanres=$matches[1][1];
		$clameffi=$matches[1][5];
		$sigeffi=$matches[1][6];
		$totalbuf=$matches[1][8];
		$sigbuff=$matches[1][9];
		
		$sql="insert into $sig_file (mval,filename,filesize,sha1val,uptime,result,ip,clamtime , sigscantime , clamres ,sigscanres, clameffi,sigeffi,totalbuf,sigbuf)"
		." values('$md5value','$filename','$filesize','$sha1value','".constant('NOWTIME')."','$result','$_SERVER[REMOTE_ADDR]','$clamtime','$sigscantime','$clamres','$sigscanres','$clameffi','$sigeffi','$totalbuf','$sigbuff')";
		
		if(!$dbo->exeUpdate($sql)){
			echo "<script>alert('数据库插入失败');location.href='index.php';</script>";
		}
		$tpl->assign("matches",$matches);
		$tpl->assign("filename",$filename);
		$tpl->assign("filetype",$filetype);
		$tpl->assign("filesize",$filesize);
		$tpl->assign("md5value",$md5value);
		$tpl->assign("sha1value",$sha1value);
		$tpl->assign("time",constant('NOWTIME'));
		$tpl->display("scan.html");
		
		}
			
	}
}
else
{
	echo "Invalid file";
}
?>

